package model;

import interfaces.MasterI;
import interfaces.TaskI;
import java.io.IOException;

import server.Server;

/**
 * Master pattern for Server-Client 
 *
 * @param <IN>
 * @param <OUT>
 * @param <INOUT>
 */
public class Master<IN, OUT, INOUT> implements MasterI {
	// private static LinkedList <TaskI> Tasklist = new LinkedList<TaskI>();
	static TaskI task = null;
	static FIFOBuffer initialdata = null;

	public static FIFOBuffer result = new FIFOBuffer();

	/**
	 * Master with cached Clientpool
	 * 
	 * @param TaskI t
	 * @param <INOUT> dataIN
	 */
	public Master(TaskI<IN, OUT> t, FIFOBuffer<INOUT> dataIN) {
		task = t;
		initialdata = dataIN;
		try {

			new Server();

		} catch (Exception e) {
			System.err.println("Master:Server error");
			e.printStackTrace();
		}

		System.out.println("Master: stop");

	}

	/**
	 * Master with restricted Clientmount
	 * 
	 * @param int poolsize
	 * @param TaskI t 
	 * @param <INOUT> dataIN
	 */
	public Master(int poolsize, TaskI<IN, OUT> t, FIFOBuffer<INOUT> dataIN) {
		task = t;
		initialdata = dataIN;
		try {
			new Server(poolsize);
		} catch (IOException e) {
			System.err.println("Master:Server error");
			e.printStackTrace();
		}

	}

	public static Object getInitialData() {
		if (initialdata.eod()) {
			// IN d=dataIN.recv();
			return null;
		} else {
			return initialdata.recv();
		}

	}

	public static TaskI getTask() {
		return task;
	}

	public void setTask(TaskI task) {
		task = task;
	}

	public FIFOBuffer getResult() {
		return result;
	}



	@Override
	public void stop() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		
	}



}
